package org.example.springwebmvc.controller;

import org.example.springwebmvc.model.MessageInfo;
import org.example.springwebmvc.service.MessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RequestMapping("/message")
@RestController
public class MessageController {

    @Autowired
    private MessageService messageService;

//    发布留言信息
    @RequestMapping("/publish")
    public Boolean publish(MessageInfo messageInfo){
        System.out.println("接受参数:"+messageInfo);
//       1. 首先 参数校验
        if (!StringUtils.hasLength(messageInfo.getFrom())|| !StringUtils.hasLength(messageInfo.getTo())
                ||!StringUtils.hasLength(messageInfo.getSay())){
            return false;
        }
//        2. 存储信息
        Integer result = messageService.publish(messageInfo);

        return result != null && result > 0;
    }

//    获取留言信息
    @RequestMapping("/getlist")
    public List<MessageInfo> getMessageInfoList() {
        return messageService.getMessageList();
    }
}
